#include <my_global.h>
#include <mysql.h>

#pragma comment(lib,"libmysql.lib")
#pragma comment(lib,"mysqlclient.lib")

//int main(int argc, char **argv)
//{
//  printf("MySQL client version: %s\n", mysql_get_client_info());
//}

void createTable(MYSQL *conn)
{
	mysql_query(conn, "CREATE TABLE writers(name VARCHAR(25))");

	mysql_query(conn, "INSERT INTO writers VALUES('Leo Tolstoy')");
	mysql_query(conn, "INSERT INTO writers VALUES('Jack London')");
	mysql_query(conn, "INSERT INTO writers VALUES('Honore de Balzac')");
	mysql_query(conn, "INSERT INTO writers VALUES('Lion Feuchtwanger')");
	mysql_query(conn, "INSERT INTO writers VALUES('Emile Zola')");
}

void getTable(MYSQL *conn)
{
	MYSQL_RES *result;
	MYSQL_ROW row;
	int num_fields;
	int i;

	mysql_query(conn, "SELECT * FROM writers");
	result = mysql_store_result(conn);

	num_fields = mysql_num_fields(result);

	while ((row = mysql_fetch_row(result)))
	{
		for(i = 0; i < num_fields; i++)
		{
			printf("%s ", row[i] ? row[i] : "NULL");
		}
		printf("\n");
	}

	mysql_free_result(result);

}

int main(int argc, char **argv)
{

	MYSQL *conn;

	conn = mysql_init(NULL);

	if (conn == NULL) {
		printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
		return 1;
	}

	if (mysql_real_connect(conn, "localhost", "root", 
		"123456", "testdb", 0, NULL, 0) == NULL) {
			printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
			//exit(1);
			return 1;
	}

	//createTable(conn);
	getTable(conn);

	mysql_close(conn);

}